/// Creator:      gaoruishan
/// CreatDate:    2025-09-01
/// Description:  患者交接
Class Nur.MNISV3.Service.PatHandover Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 获取交接列表接口
/// w ##class(Nur.MNISV3.Service.PatHandover).GetConnectListV4("2025-08-25","2025-09-03","","3","","20665")
/// w ##class(Nur.MNISV3.Service.PatHandover).GetConnectListV4("2025-08-25","2025-09-03","","3","0000000689","20665")
ClassMethod GetConnectListV4(startDate, endDate, type, wardId, regNo = "", userId = "") As %String
{
    n (startDate,endDate,type,wardId,regNo,userId)
    s ^GRS("GetConnectListV4")=$lb(startDate,endDate,type,wardId,regNo,userId)
    s $zt="errGetConnectListV4"
    s msg="成功",msgcode="999999",status="0"
    i (startDate'="") s startDate=##class(websys.Conversions).DateHtmlToLogical(startDate)
    e  s startDate=$p($h,",",1)
    i (endDate'="") s endDate=##class(websys.Conversions).DateHtmlToLogical(endDate)
    e  s endDate=$p($h,",",1)
    s list=##class(%ListOfDataTypes).%New()
    i (regNo'="") { // 扫码 即单个患者
        s getAdmId=..GetAdmId(regNo,wardId) //获取就诊号
        i ($p(getAdmId,"^",1)="") q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($p(getAdmId,"^",3),$p(getAdmId,"^",4),$p(getAdmId,"^",5))
        s admId=$p(getAdmId,"^",1)
        s list=..GetPHODatas(admId,userId,type,startDate,endDate)
    } else { // 病区患者列表
        s rs=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
        d rs.Execute(wardId,userId,"","")
        while(rs.Next()){
            s episodeId=rs.GetDataByName("episodeId")
            s phoList=..GetPHODatas(episodeId,userId,type,startDate,endDate)
            f i=1:1:phoList.Count() { //循环添加到list
                s phoData=phoList.GetAt(i)
                d list.Insert(phoData)
            }
        }
    }
    s typeStr=..GetTypeStr(wardId)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt(msg,"msg")
    d json.SetAt(msgcode,"msgcode")
    d json.SetAt(status,"status")
    d json.SetAt(typeStr,"connectType")
    d json.SetAt(list,"connectAllList")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetConnectListV4
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($ze,"108121","-1")
}

/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.PatHandover).GetPHODatas("1217","20665","","2025-08-25","2025-09-03"))
ClassMethod GetPHODatas(episodeId, userId, type = "", startDate, endDate)
{
    n (episodeId,userId,type,startDate,endDate)
    s ^GRS("GetPHODatas")=$lb(episodeId,userId,type,startDate,endDate)
    s $zt="errGetPHODatas"
    i (startDate["-") s startDate=##class(websys.Conversions).DateHtmlToLogical(startDate)
    i (endDate["-") s endDate=##class(websys.Conversions).DateHtmlToLogical(endDate)
    s list=##class(%ListOfDataTypes).%New()
    s bedCode=##class(Nur.NIS.Service.Base.Patient).GetBedCode(episodeId)
    s name=##class(Nur.NIS.Service.Base.Patient).GetName(episodeId)
    s rN=##class(Nur.NIS.Service.Base.Patient).GetRegNo(episodeId)
    s par=0 
    f  {
        s par=$o(^Nur.MNISV3.PHOFlowI("RegNo"," "_rN,par)) q:(par="")  
        s flowData=$g(^Nur.MNISV3.PHOFlowD(par)) // RegNo交接记录表
        q:(flowData="")
        s typeId1=$lg(flowData,3)
        q:($g(type)'="")&&(type'=typeId1)
        s recDate=$lg(flowData,5)
        s recTime=$lg(flowData,6)
        s recUser=$lg(flowData,7)
        w recDate,startDate,endDate,!
        q:(recDate<startDate)
        s lastRecDate=recDate
        s chl=$o(^Nur.MNISV3.PHOStepD(par,""),-1)
        i (chl'="")  {
            s stepData=$g(^Nur.MNISV3.PHOStepD(par,chl))
            q:(stepData="")
            s lastRecDate=$lg(stepData,4)
        }
        q:(lastRecDate>endDate)
        s (userName,typeName)=""
        i (recUser'="") s userName=$p($g(^SSU("SSUSR",recUser)),"^",2)
        i (typeId1'="") {
            s typeData=$g(^CF.NUR.MNISV3.PHOTypeD(typeId1))
            q:(typeData="")  //过滤类型
            s typeName=$lg(typeData,2)
        }
        s canDeleteFlag=..GetCanDeleteFlag(par,userId)
        s stepList=##class(%ListOfDataTypes).%New()
        s chl=0 
        f  {
            s chl=$o(^Nur.MNISV3.PHOStepD(par,chl))
            q:(chl="")  
            s stepData=$g(^Nur.MNISV3.PHOStepD(par,chl))
            q:(stepData="")
            s nodeId1=$lg(stepData,2)
            s recDate=$lg(stepData,4)
            s recTime=$lg(stepData,5)
            s recUser=$lg(stepData,6)
            s (userName,nodeName)=""
            i (recUser'="") s userName=$p($g(^SSU("SSUSR",recUser)),"^",2)
            i (nodeId1'="") {
                i (nodeId1["||") s nodePar=$p(nodeId1,"||",1),nodeChl=$p(nodeId1,"||",2)
                e  s nodePar=typeId1,nodeChl=nodeId1
                q:(nodePar="")||(nodeChl="")
                s nodeData=$g(^CF.NUR.MNISV3.PHONodeD(nodePar,nodeChl))
                q:(nodeData="")
                s nodeName=$lg(nodeData,2)
            }
            s stepObject=##class(%ArrayOfDataTypes).%New()
            d stepObject.SetAt(nodeName,"rigSubType")
            d stepObject.SetAt($zd(recDate,3),"rigSubDate")
            d stepObject.SetAt($zt(recTime,2),"rigSubTime")
            d stepObject.SetAt(userName,"firstUser")
            d stepList.Insert(stepObject)
        }
        s object=##class(%ArrayOfDataTypes).%New()
        d object.SetAt(bedCode,"bedCode")
        d object.SetAt(name,"name")
        d object.SetAt(rN,"regNo")
        d object.SetAt(stepList,"record")
        d object.SetAt(typeName,"rigType")
        d object.SetAt(typeId1,"rigTypeId")
        d object.SetAt($zd(recDate,3),"rigDate")
        d object.SetAt($zt(recTime,2),"rigTime")
        d object.SetAt(userName,"rigUser")
        d object.SetAt($g(par),"parentRec")
        d object.SetAt(canDeleteFlag,"canDeleteFlag")
        d list.Insert(object)
    }
    q list
errGetPHODatas
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($ze,"108121","-1")
}

/// 扫码获取或者输入登记号
/// w ##class(Nur.MNISV3.Service.PatHandover).GetScanConnectV4("0000000689","3")
ClassMethod GetScanConnectV4(regNo, wardId = "") As %String
{
    n (regNo,wardId)
    s ^GRS("GetScanConnectV4")=$lb(regNo,wardId)
    s $zt="errGetScanConnectV4"
    s msg="成功",msgcode="999999",status="0"
    s getAdmId=..GetAdmId(regNo,wardId)
    s regNo=$p(getAdmId,"^",2)
    s endFlag="N",(curType,curNode,curFlow,type,node)=""
    i (regNo'="") d
    .s current=..GetCurType(regNo)
    .s curType=current.GetAt("typeName")
    .q:(curType="")
    .s endFlag=current.GetAt("endFlag")
    .s curType=curType_":"_current.GetAt("type")
    .s curNode=current.GetAt("nodeName")
    .s curFlow=current.GetAt("par")
    .s type=current.GetAt("type")
    .s node=current.GetAt("node")
    i (node'="") d
    .s nodeStr=..GetNodeStr(wardId,type,"")
    .s nodeLength=$l(nodeStr,"^")
    .s nodePiece=$p(nodeStr,"^",nodeLength)
    .s nodePieceChl=$p(nodePiece,":",2)
    .s nodeChl=$p(node,"||",2)
    .i (nodeChl=nodePieceChl) s endFlag="Y"
    s typeStr=..GetTypeStr(wardId) // 所有的节点类型
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt(msg,"msg")
    d json.SetAt(msgcode,"msgcode")
    d json.SetAt(status,"status")
    d json.SetAt(typeStr,"connectType") // "手术:1^转科:2"
    d json.SetAt(endFlag,"ifEndConnect")
    d json.SetAt(curType,"nowNode") // 现在的节点
    d json.SetAt(curNode,"nowSubNode")
    d json.SetAt(curFlow,"parentRec") // 有记录 继续完成?
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetScanConnectV4
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($ze,"108121","-1")
}

/// 登记患者--新增交接记录
/// w ##class(Nur.MNISV3.Service.PatHandover).SaveConnectV4("0000000689","1","20665","3")
ClassMethod SaveConnectV4(regNo, type, userId, wardId) As %String
{
    n (regNo,type,userId,wardId)
    s ^GRS("SaveConnectV4")=$lb(regNo,type,userId,wardId)
    s $zt="errSaveConnectV4"
    s msg="成功",msgcode="999999",status="0"
    i (type="") q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("配置错误","108121","-1")
    s getAdmId=..GetAdmId(regNo,wardId)
    i ($p(getAdmId,"^",1)="") q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($p(getAdmId,"^",3),$p(getAdmId,"^",4),$p(getAdmId,"^",5))
    s regNo=$p(getAdmId,"^",2)
    ts
    s object=##class(Nur.MNISV3.PHOFlow).%New()
    s object.RegNo=regNo
    s object.PHOType=type
    s object.EndFlag="N"
    s object.RecDate=$p($h,",",1)
    s object.RecTime=$p($h,",",2)
    s object.RecUser=userId
    s tc=object.%Save()
    i ($$$ISOK(tc)) tc
    e  tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("保存失败","108121","-1")
    s id=object.%Id()
    d object.%Close()
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt(msg,"msg")
    d json.SetAt(msgcode,"msgcode")
    d json.SetAt(status,"status")
    d json.SetAt(id,"retId") // 记录 parentId
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errSaveConnectV4
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($ze,"108121","-1")
}

/// 获取模板和病人信息
/// 1.移动到当前已保存节点的下一个节点，如果为空返回首个，如果末个返回空
/// 2.增加subType默认为空，选择时跳到对应步骤
/// w ##class(Nur.MNISV3.Service.PatHandover).GetConnectAndPatV4("0000000689","1","","3","20665","23")
ClassMethod GetConnectAndPatV4(regNo, type, subType = "", wardId = "", userId = "", groupId = "") As %String
{
    n (regNo,type,subType,wardId,userId,groupId)
    s ^GRS("GetConnectAndPatV4")=$lb(regNo,type,subType,wardId,userId,groupId)
    s $zt="errGetConnectAndPatV4"
    s msg="成功",msgcode="999999",status="0"
    s getAdmId=..GetAdmId(regNo,wardId)
    i ($p(getAdmId,"^",1)="") q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($p(getAdmId,"^",3),$p(getAdmId,"^",4),$p(getAdmId,"^",5))
    s admId=$p(getAdmId,"^",1)
    s regNo=$p(getAdmId,"^",2)
    s patInfo=##class(Nur.MNISV3.Service.PatientInfo).GetPatient(admId)
    //当前type的所有节点
    s nodeStr=..GetNodeStr(wardId,type,"")
    s phaseInfo=..GetPhaseInfoByRecOrSel("",regNo,type,subType,wardId,userId,groupId)
    w ##class(Nur.JSON).Encode(phaseInfo),!
    s nextNodeChl=phaseInfo.GetAt("nextNodeChl")
    s canEditFlag=phaseInfo.GetAt("canEditFlag")
    i (subType'="") s nextNodeChl=subType 
    s nextNodeId=$s(nextNodeChl'="":(type_"||"_nextNodeChl),1:"") //拼接1||1
    w nextNodeId,!
    // 获取模板配置信息
    s nextNodeModelInfo=..GetModelInfoByNodeId(nextNodeId)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt(msg,"msg")
    d json.SetAt(msgcode,"msgcode")
    d json.SetAt(status,"status")
    d json.SetAt(nodeStr,"allChildRec")
    d json.SetAt(nextNodeChl,"childRec")
    d json.SetAt(patInfo,"patInfo")
    //患者信息 拼接字符串
    s patInfoStr="登记号："_patInfo.GetAt("regNo")_"<br/> 姓名："_patInfo.GetAt("name")_"<br/> 性别："_patInfo.GetAt("sex")_$c(10)_"     年龄："_patInfo.GetAt("age")
    _"<br/> 科室："_patInfo.GetAt("ctLocDesc")_"<br/> 姓名："_patInfo.GetAt("wardDesc")
    d json.SetAt(patInfoStr,"patInfoStr")
    d json.SetAt(nextNodeModelInfo,"modelInfo")
    d json.SetAt(canEditFlag,"canEditFlag")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetConnectAndPatV4
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($ze,"108121","-1")
}

/// 保存录入信息
/// w ##class(Nur.MNISV3.Service.PatHandover).SaveConnectSubV4("1","0000000689","1","3","20665","23","{""P"":""55"",""R"":""66"",""HODate"":""2025-09-01"",""Notice"":""hdhdh"",""T"":""36.5"",""Needle"":""否"",""Catheter"":""鼻导管"",""HOTime"":""11:47"",""DP"":""88"",""HOUser"":""hs01"",""SP"":""77"",""HRUser"":""hs01""}")
ClassMethod SaveConnectSubV4(parentId, regNo, type, wardId, userId = "", groupId = "", formJson = "") As %String
{
    n (parentId,regNo,type,wardId,userId,groupId,formJson)
    s ^GRS("SaveConnectSubV4")=$lb(parentId,regNo,type,wardId,userId,groupId,formJson)
    s $zt="errSaveConnectSubV4"
    s msg="成功",msgcode="999999",status="0"
    s flowId=parentId,nodeChl=type,typeId=""
    i (flowId'="") d
    .s flowData=$g(^Nur.MNISV3.PHOFlowD(flowId))
    .s typeId=$lg(flowData,3)
    i (typeId="")||(nodeChl="") q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("配置错误","108121","-1")
    s nodeId=typeId_"||"_nodeChl
    s getAdmId=..GetAdmId(regNo,wardId)
    s admId=$p(getAdmId,"^",1)
    i (admId="") q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($p(getAdmId,"^",3),$p(getAdmId,"^",4),$p(getAdmId,"^",5))
    s regNo=$p(getAdmId,"^",2)
    s phaseInfo=..GetPhaseInfoByRecOrSel(flowId,"","",nodeChl,wardId,userId,groupId)
    s canEditFlag=phaseInfo.GetAt("canEditFlag")
    //i (canEditFlag="N") q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("不能回退记录","108121","-1")
    s date=$p($h,",",1)
    s time=$p($h,",",2)
    ts
    s stepObject=##class(Nur.MNISV3.PHOStep).%New()
    s stepObject.PHOFlow=##class(Nur.MNISV3.PHOFlow).%OpenId(flowId)
    s stepObject.PHONode=nodeId
    s stepObject.EndFlag="N"
    s stepObject.RecDate=date
    s stepObject.RecTime=time
    s stepObject.RecUser=userId
    s object=##class(Nur.MNISV3.Common.JSON).Decode(formJson)
    //取模板 
    s modelObject=..GetModelInfoByNodeId(nodeId)
    s modelId=modelObject.GetAt("modelId")
    s itemList=modelObject.GetAt("itemList")
    s itemCount=itemList.Count()
    f idx=1:1:itemCount {
        s itemObject=itemList.GetAt(idx)
        s itemCode=itemObject.GetAt("ItemCode")
        s itemId=itemObject.GetAt("itemId")
        s value=object.GetAt(itemCode)
        i itemId["||" { //分隔字符 第一个是类型
            s itemType=$p(itemId,"||",1)
            s itemIndex=$p(itemId,"||",2)
            s itm="Item"_itemIndex  
            if $g(value)'="" s $ZOBJPROPERTY(stepObject,itm)=value
        }
    }
    i $g(itemType)'="" s $ZOBJPROPERTY(stepObject,"ItemType")=itemType
    s tc=stepObject.%Save()
    i ('$$$ISOK(tc)) s error="保存失败"
    i ($g(error)'="") tro  q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(error,"108121","-1")
    e  tc
    s stepId=stepObject.%Id()
    d stepObject.%Close()
    // 插入护理病历
    //d ..SaveToNurEmr(admId,stepId,formJson)
    
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt(msg,"msg")
    d json.SetAt(msgcode,"msgcode")
    d json.SetAt(status,"status")
    d json.SetAt(stepId,"retId")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errSaveConnectSubV4
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson($ze,"108121","-1")
}

/// 通过登记号 获取就诊号
ClassMethod GetAdmId(regNo, wardId) As %String
{
    n (regNo,wardId)
    s barcodeFlag=##class(Nur.MNISV3.Service.Barcode).GetBarcodeFlag(regNo,"","",wardId,.outputBarcode,.barcodeMessage,.ordFlag) // EH 条码设置
    i (barcodeFlag'="PAT") q "^^未找到患者^100121^-1"
    i (outputBarcode'="") s regNo=outputBarcode
    s admIdStr=##class(Nur.MNISV3.Service.Patient).GetPatAdm(regNo)
    s admId=$p(admIdStr,"^",1)
    i (admId="") q "^^未找到患者^100322^-1"
    s visitStatus=$p($g(^PAADM(admId)),"^",20)
    i (visitStatus="D") q "^^患者已出院^104999^-1"
    q admId_"^"_regNo
}

/// 获取是否能删除
ClassMethod GetCanDeleteFlag(flowId, userId = "") As %String
{
    n (flowId,userId)
    s stepCount=0,lastRecUserId="",canEditFlag="Y"
    s stepChl=0 f  s stepChl=$o(^Nur.MNISV3.PHOStepD(flowId,stepChl)) q:(stepChl="")  d
    .s stepData=$g(^Nur.MNISV3.PHOStepD(flowId,stepChl))
    .s lastRecUserId=$lg(stepData,6)
    .s stepCount=$i(stepCount) // 有数量-不能删
    i (stepCount>1) s canEditFlag="N"
    i (userId'=lastRecUserId)&&(stepCount) s canEditFlag="N" //不是当前用户-不能删
    q canEditFlag
}

/// 获取交接类型--名称:ID  例如: 手术:1^转科:2
/// w ##class(Nur.MNISV3.Service.PatHandover).GetTypeStr(3)
ClassMethod GetTypeStr(wardId) As %String
{
    n (wardId)
    s typeStr=""
    s hospId=##class(Nur.MNISV3.Service.Config).GetModuleMainHospByLocID("","",wardId,"")
    i ('$d(^CF.NUR.MNISV3.PHOTypeI("Code"," "_hospId))) s hospId=$o(^CF.NUR.MNISV3.PHOTypeI("Code"," "_hospId)),hospId=$e(hospId,2,$l(hospId))
    s code="" f  s code=$o(^CF.NUR.MNISV3.PHOTypeI("Code"," "_hospId," "_code)),code=$e(code,2,$l(code)) q:(code="")  d
    .s par=0 f  s par=$o(^CF.NUR.MNISV3.PHOTypeI("Code"," "_hospId," "_code,par)) q:(par="")  d
    ..s typeData=$g(^CF.NUR.MNISV3.PHOTypeD(par))
    ..q:(typeData="")
    ..s typeName=$lg(typeData,2)
    ..s typeDesc=typeName_":"_par // ^拼接字符串
    ..i (typeStr'="") s typeStr=typeStr_"^"
    ..s typeStr=typeStr_typeDesc
    q typeStr
}

/// 获取当前流程--- 类型,节点 记录id 明细id
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.PatHandover).GetCurType("0000000689"))
/// 空数据: {"chl":"","endFlag":"N","node":"","nodeName":"","par":"","type":"","typeName":""}
ClassMethod GetCurType(regNo) As %ArrayOfDataTypes
{
    n (regNo)
    s (type,node,typeName,nodeName,chl)="",endFlag="N"
    s par=$o(^Nur.MNISV3.PHOFlowI("RegNo"," "_regNo,""),-1)
    i (par'="") d
    .s flowData=$g(^Nur.MNISV3.PHOFlowD(par))
    .q:(flowData="")
    .s typeId1=$lg(flowData,3)
    .s endFlag=$lg(flowData,4)
    .q:(typeId1="")
    .s typeData=$g(^CF.NUR.MNISV3.PHOTypeD(typeId1))
    .q:(typeData="")
    .s type=typeId1
    .s typeName=$lg(typeData,2)
    .s chl=$o(^Nur.MNISV3.PHOStepD(par,""),-1)
    .i (chl>0) d
    ..s stepData=$g(^Nur.MNISV3.PHOStepD(par,chl))
    ..s nodeId1=$lg(stepData,2)
    ..s nodePar=$p(nodeId1,"||",1)
    ..s nodeChl=$p(nodeId1,"||",2)
    ..s nodeData=$g(^CF.NUR.MNISV3.PHONodeD(nodePar,nodeChl))
    ..q:(nodeData="")
    ..s node=nodeId1
    ..s nodeName=$lg(nodeData,2)
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt(endFlag,"endFlag")
    d json.SetAt(type,"type") //类型id 和名称
    d json.SetAt(typeName,"typeName")
    d json.SetAt(node,"node") //节点Id 和名称
    d json.SetAt(nodeName,"nodeName")
    d json.SetAt(par,"par") //记录id
    d json.SetAt(chl,"chl") //记录明细Id
    q json
}

/// 获取当前流程记录中 对应节点
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.PatHandover).GetCurNode("0000000689","1","23"))
ClassMethod GetCurNode(regNo, type = "", groupId = "") As %ArrayOfDataTypes
{
    n (regNo,type,groupId)
    s ^GRS("GetCurNode")=$lb(regNo,type,groupId)
    s node="",endFlag="N",(typeFlag,nodeFlag)=0
    s par="" f  s par=$o(^Nur.MNISV3.PHOFlowI("RegNo"," "_regNo,par),-1) q:(par="")||(typeFlag=1)  d
    .s flowData=$g(^Nur.MNISV3.PHOFlowD(par))
    .q:(flowData="")
    .s typeId1=$lg(flowData,3)
    .s endFlag=$lg(flowData,4)
    .q:(typeId1="")
    .s typeData=$g(^CF.NUR.MNISV3.PHOTypeD(typeId1))
    .q:(typeData="")
    .q:(type'=typeId1)
    .s typeFlag=1
    .s nodePar=par
    .s chl="" f  s chl=$o(^Nur.MNISV3.PHOStepD(par,chl),-1) q:(chl="")||(nodeFlag=1)  d
    ..s stepData=$g(^Nur.MNISV3.PHOStepD(par,chl))
    ..s nodeId1=$lg(stepData,2)
    ..q:(nodeId1="")
    ..s nodePar=$p(nodeId1,"||",1)
    ..s nodeChl=$p(nodeId1,"||",2)
    ..s nodeData=$g(^CF.NUR.MNISV3.PHONodeD(nodePar,nodeChl))
    ..q:(nodeData="")
    ..s nodeFlag=1
    ..s node=nodeId1
    ..s endFlag=$lg(stepData,3)
    i node'="" { //不为空
        s nodePar=$p(node,"||",1)
        s nodeChl=$p(node,"||",2)
    }
    s json=##class(%ArrayOfDataTypes).%New()
    d json.SetAt($g(endFlag),"endFlag")
    d json.SetAt($g(nodePar),"par")
    d json.SetAt($g(nodeChl),"chl")
    q json
}

/// 获取对应节点str 例如: 入手术室:1^出手术室:2
/// w ##class(Nur.MNISV3.Service.PatHandover).GetNodeStr("3","1","")
ClassMethod GetNodeStr(wardId, type = "", groupId = "") As %String
{
    n (wardId,type,groupId)
    s ^GRS("GetNodeStr")=$lb(wardId,type,groupId)
    s nodeStr="",(typeFlag,nodeFlag)=0
    s hospId=##class(Nur.MNISV3.Service.Config).GetModuleMainHospByLocID("","",wardId,"")
    i ('$d(^CF.NUR.MNISV3.PHOTypeI("Code"," "_hospId))) s hospId=$o(^CF.NUR.MNISV3.PHOTypeI("Code"," "_hospId)),hospId=$e(hospId,2,$l(hospId))
    s code="" f  s code=$o(^CF.NUR.MNISV3.PHOTypeI("Code"," "_hospId," "_code)),code=$e(code,2,$l(code)) q:(code="")||(typeFlag=1)  d
    .s par=0 f  s par=$o(^CF.NUR.MNISV3.PHOTypeI("Code"," "_hospId," "_code,par)) q:(par="")||(typeFlag=1)  d
    ..s typeData=$g(^CF.NUR.MNISV3.PHOTypeD(par))
    ..q:(typeData="")
    ..q:(type'=par)
    ..w par,!
    ..s typeFlag=1
    ..s chl=0 f  s chl=$o(^CF.NUR.MNISV3.PHONodeD(par,chl)) q:(chl="")||(nodeFlag=1)  d
    ...s nodeData=$g(^CF.NUR.MNISV3.PHONodeD(par,chl))
    ...q:(nodeData="")
    ...s nodeName=$lg(nodeData,2)
    ...s nodeDesc=nodeName_":"_chl // ^拼接字符串
    ...i (nodeStr'="") s nodeStr=nodeStr_"^"
    ...s nodeStr=nodeStr_nodeDesc
    ...i (groupId'="") s nodeFlag=1
    q nodeStr
}

/// 获取步骤信息 通过记录
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.PatHandover).GetPhaseInfoByRecOrSel("","0000000689","1","","3","20665","23"))
/// {"canEditFlag":"Y","endFlag":"N","nextEndFlag":"N","nextNodeChl":"","nodeChl":"","typeId":1}
ClassMethod GetPhaseInfoByRecOrSel(flowId, regNo = "", typeId = "", selNodeChl = "", wardId = "", userId = "", groupId = "") As %ArrayOfDataTypes
{
    n (flowId,regNo,typeId,selNodeChl,wardId,userId,groupId)
    s ^GRS("GetPhaseInfoByRecOrSel")=$lb(flowId,regNo,typeId,selNodeChl,wardId,userId,groupId)
    s (nodeChl,selNodeStepChl)=""
    i (flowId'="") d //有记录id
    .s flowData=$g(^Nur.MNISV3.PHOFlowD(flowId))
    .s regNo=$lg(flowData,2)
    .s typeId=$lg(flowData,3)
    .s endFlag=$lg(flowData,4)
    .s stepChl="" f  s stepChl=$o(^Nur.MNISV3.PHOStepD(flowId,stepChl),-1) q:(stepChl="")||(selNodeStepChl'="")  d
    ..s stepData=$g(^Nur.MNISV3.PHOStepD(flowId,stepChl))
    ..s nodeId=$lg(stepData,2)
    ..s nodeChl=$p(nodeId,"||",2)
    ..i (nodeChl=selNodeChl) s selNodeStepChl=stepChl
    .s stepChl=$o(^Nur.MNISV3.PHOStepD(flowId,""),-1)
    .i (stepChl'="") d
    ..s stepData=$g(^Nur.MNISV3.PHOStepD(flowId,stepChl))
    ..s nodeId=$lg(stepData,2)
    ..s nodeChl=$p(nodeId,"||",2)
    e  d //没有Id  取当前节点
    .s curNodeInfo=..GetCurNode(regNo,typeId,groupId)
    .s endFlag=curNodeInfo.GetAt("endFlag")
    .s nodeChl=curNodeInfo.GetAt("chl")
    w "nodeChl="_nodeChl,!
    s nextNodeChl="",(nextNodeIndex,selNodeChlIndex)=-1,canEditFlag="Y",nextEndFlag="N"
    s nodeStr=..GetNodeStr(wardId,typeId,"")
    w "nodeStr="_nodeStr,!
    // 遍历 所有节点
    s nodeLength=$l(nodeStr,"^")
    f idx=1:1:nodeLength d
    .s nodePiece=$p(nodeStr,"^",idx)
    .s nodePieceChl=$p(nodePiece,":",2)
    .i (selNodeChl=nodePieceChl) s selNodeChlIndex=idx
    .i (nodeChl="")&&(nextNodeChl="") s nextNodeChl=nodePieceChl //如果nodeChl空, 默认第一个
    .e  i (nodeChl=nodePieceChl)&&(idx<nodeLength) d
    ..s nextNodeIndex=idx+1
    ..s nextNodePiece=$p(nodeStr,"^",nextNodeIndex)
    ..s nextNodePieceChl=$p(nextNodePiece,":",2)
    ..s nextNodeChl=nextNodePieceChl
    i (selNodeChlIndex<nextNodeIndex)&&(nextNodeIndex'=-1) s canEditFlag="N"
    i (nextNodeChl'="") d
    .s lastNodePiece=$p(nodeStr,"^",nodeLength)
    .s lastNodePieceChl=$p(lastNodePiece,":",2)
    .i (nextNodeChl=lastNodePieceChl) s nextEndFlag="Y"
    s object=##class(%ArrayOfDataTypes).%New()
    d object.SetAt(typeId,"typeId") // 类型id
    d object.SetAt(nodeChl,"nodeChl")
    d object.SetAt(endFlag,"endFlag")
    d object.SetAt(nextNodeChl,"nextNodeChl") //下一个节点id
    d object.SetAt(nextEndFlag,"nextEndFlag")
    d object.SetAt(canEditFlag,"canEditFlag")
    q object
}

/// 获取模板信息 通过节点Id
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.PatHandover).GetModelInfoByNodeId("1||1",""))
ClassMethod GetModelInfoByNodeId(nodeId1, admId1 = "") As %ArrayOfDataTypes
{
    n (nodeId1,admId1)
    s ^GRS("GetModelInfoByNodeId")=$lb(nodeId1,admId1)
    s modelObject=##class(%ArrayOfDataTypes).%New()
    s modelLinkId=""
    i (nodeId1["||") d
    .s nodePar=$p(nodeId1,"||",1),nodeChl=$p(nodeId1,"||",2)
    .s nodeData=$g(^CF.NUR.MNISV3.PHONodeD(nodePar,nodeChl))
    .s modelLinkId=$lg(nodeData,7)
    i (modelLinkId="") q modelObject
    w modelLinkId,!
    i $g(hospitalId)="" s hospitalId="2"
    //获取 再合并
    s itemList=##class(%ListOfDataTypes).%New() 
    s subId=""
    f i=1:1:$l(modelLinkId,"!") //遍历可操作节点
    {
        s formCode=$p(modelLinkId,"!",i)
        continue:formCode=""
        s formCode=$ZCVT(formCode,"U") //Form组件索引
        s formId=$o(^CF.NUR.MNISV3.ModelFormI("Code"," "_hospitalId," "_formCode,""))
        continue:formId=""
        s formData=##class(CF.NUR.MNISV3.ModelForm).GetModelInfoSingleByModelCode(formCode,hospitalId)
        s ModelCode=formData.GetAt("FormCode")
        s ModelName=formData.GetAt("FormName")
        s modelId=formData.GetAt("ID")
        s list=formData.GetAt("itemList")
        f idx=1:1:list.Count() {
            d itemList.Insert(list.GetAt(idx)) //插入form
        }
    
    }
    s modelInfo=##class(%ArrayOfDataTypes).%New()
    d modelInfo.SetAt($g(itemList),"itemList")
    d modelInfo.SetAt($g(ModelCode),"ModelCode")
    d modelInfo.SetAt($g(ModelName),"ModelName")
    d modelInfo.SetAt($g(modelId),"modelId")
    q modelInfo
}

}
